<div class="container" style="text-align: center;">
  <form class="form" onsubmit="return signup()">
    <h2 class="form-heading translateit">Sign up for cdnjs</h2>
    <p>Save your
      <strong>favorites</strong> for easy access | Receive important system
      <strong>notifications | </strong>Create your own
      <strong>public profile</strong> to share.</p>
    <!--<br />
  Post updates about <strong>your projects</strong> to over 40,000 developers a week</p>-->

    <br />
    <br />
    <div class="form-fields">
      <input id="username" type="text" class="form-control" placeholder="Username" required>
      <br />
      <input id="email" type="text" class="form-control" placeholder="Email address" required>
      <br />
      <input id="password" type="password" class="form-control" placeholder="Password" required>
      <br />
    </div>
    <button class="btn btn-lg btn-info btn-block translateit" type="submit">Create my account</button>
    <img id="loader" src="img/ajax-loader.gif" style="display: none; margin: 30px auto;">
  </form>
  <p>
    <br /><span>Already have an account?</span> <a href="login">Log in</a>
  </p><br />
  <div  class='col-md-6 col-md-offset-3'>

  <p style="color: #444"><a href="https://www.userapp.io" target="_blank"><img src="https://www.userapp.io/img/badge.png" title="User management by UserApp"></a><br /><span>Our user system is sponsored by UserApp. UserApp provides you with user management functionality that results in faster development, faster revenue, more users, and the ability to serve your users better by engaging with them more efficiently.</span></p>
</div>
</div>
<script>
// Function to show and hide the loader anim

function showLoader(show) {
  document.getElementById("loader").style.display = (show ? "block" : "none");
}

function onLoginSuccessful() {
  // Now, save the token in a cookie
  Kaka.set("ua_session_token", UserApp.global.token);

  // Redirect the user to the index page
  window.location.href = "/";
  showLoader(false);
}

function signup() {
  // Show the loader
  showLoader(true);
  Kaka.remove("ua_session_token");
  // This will sign up the user
  UserApp.User.save({
    login: document.getElementById("username").value,
    email: document.getElementById("email").value,
    password: document.getElementById("password").value
  }, function(error, user) {
    if (error) {
      alert("Error: " + error.message);
    } else {

      UserApp.User.login({
        login: document.getElementById("username").value,
        password: document.getElementById("password").value
      }, function(error, result) {
        if (error) {
          // Wrong password maybe?
          alert("Error: " + error.message);
          showLoader(false);
        } else {
          $.ajax('/newregistration/' + document.getElementById("username").value, {
            success: function () {
              onLoginSuccessful();
            }
          })
        }
      });
    }

    showLoader(false);
  });

  return false;
}
</script>
